package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.bean.ResourceShipBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/9/19.
 */

public class ResourceShipAdapter extends BaseAdapter {
	private List<CustomerAndResListBean> list = new ArrayList<>();
	private Context context;
	private int height;
	private boolean isNumOne;


	public ResourceShipAdapter(List<CustomerAndResListBean> list, Context context, int height , boolean isNumOne) {
		this.list = list;
		this.context = context;
		this.height = height;
		this.isNumOne = isNumOne;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		List<CustomerAndResListBean.Customers> customerses = new ArrayList<>();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.rescource_ship_item_layout, null);
			viewHolder = new ViewHolder();
			viewHolder.mLayout = (RelativeLayout) convertView.findViewById(R.id.layout);
			viewHolder.mNameTv = (TextView) convertView.findViewById(R.id.tv);
			viewHolder.mTvLayout = (LinearLayout) convertView.findViewById(R.id.textview_layout);
			viewHolder.mIv = (ImageView) convertView.findViewById(R.id.iv);
			viewHolder.mTv1 = (TextView) convertView.findViewById(R.id.tv1);
			viewHolder.mTv2 = (TextView) convertView.findViewById(R.id.tv2);
			viewHolder.mTv3 = (TextView) convertView.findViewById(R.id.tv3);
			viewHolder.mTv4 = (TextView) convertView.findViewById(R.id.tv4);
			viewHolder.mTv5 = (TextView) convertView.findViewById(R.id.tv5);
			viewHolder.mTv6 = (TextView) convertView.findViewById(R.id.tv6);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (list != null && list.size()>0) {
			if (isNumOne && position == 0) {
				viewHolder.mLayout.setBackgroundColor(context.getResources().getColor(R.color.common_color_c12_836fdb));
			} else {
				viewHolder.mLayout.setBackgroundColor(context.getResources().getColor(R.color.common_color_c13_23c0b1));
			}
			if (list.get(position) != null && list.get(position).getName() != null) {
				if (list.get(position).getList() != null && list.get(position).getList().size() > 0) {
					for (int i = 0; i < list.get(position).getList().size(); i++) {
						if (list.get(position).getList().get(i).isSelected()) {
							customerses.add(list.get(position).getList().get(i));
						}
					}
				}
				if (customerses.size() == 0) {
					viewHolder.mTvLayout.setVisibility(View.GONE);
				} else {
					viewHolder.mTvLayout.setVisibility(View.VISIBLE);
					if (customerses.size() == 1) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText("");
						viewHolder.mTv3.setText("");
						viewHolder.mTv4.setText("");
						viewHolder.mTv5.setText("");
						viewHolder.mTv6.setText("");
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(20);
						viewHolder.mTv3.getBackground().setAlpha(20);
						viewHolder.mTv4.getBackground().setAlpha(20);
						viewHolder.mTv5.getBackground().setAlpha(20);
						viewHolder.mTv6.getBackground().setAlpha(20);
					} else if (customerses.size() == 2) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText(customerses.get(1).getName());
						viewHolder.mTv3.setText("");
						viewHolder.mTv4.setText("");
						viewHolder.mTv5.setText("");
						viewHolder.mTv6.setText("");
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(255);
						viewHolder.mTv3.getBackground().setAlpha(20);
						viewHolder.mTv4.getBackground().setAlpha(20);
						viewHolder.mTv5.getBackground().setAlpha(20);
						viewHolder.mTv6.getBackground().setAlpha(20);
					} else if (customerses.size() == 3) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText(customerses.get(1).getName());
						viewHolder.mTv3.setText(customerses.get(2).getName());
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(255);
						viewHolder.mTv3.getBackground().setAlpha(255);
						viewHolder.mTv4.getBackground().setAlpha(20);
						viewHolder.mTv5.getBackground().setAlpha(20);
						viewHolder.mTv6.getBackground().setAlpha(20);
						viewHolder.mTv4.setText("");
						viewHolder.mTv5.setText("");
						viewHolder.mTv6.setText("");
					} else if (customerses.size() == 4) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText(customerses.get(1).getName());
						viewHolder.mTv3.setText(customerses.get(2).getName());
						viewHolder.mTv4.setText(customerses.get(3).getName());
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(255);
						viewHolder.mTv3.getBackground().setAlpha(255);
						viewHolder.mTv4.getBackground().setAlpha(255);
						viewHolder.mTv5.getBackground().setAlpha(20);
						viewHolder.mTv6.getBackground().setAlpha(20);
						viewHolder.mTv5.setText("");
						viewHolder.mTv6.setText("");
					} else if (customerses.size() == 5) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText(customerses.get(1).getName());
						viewHolder.mTv3.setText(customerses.get(2).getName());
						viewHolder.mTv4.setText(customerses.get(3).getName());
						viewHolder.mTv5.setText(customerses.get(4).getName());
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(255);
						viewHolder.mTv3.getBackground().setAlpha(255);
						viewHolder.mTv4.getBackground().setAlpha(255);
						viewHolder.mTv5.getBackground().setAlpha(255);
						viewHolder.mTv6.getBackground().setAlpha(20);
						viewHolder.mTv6.setText("");
					} else if (customerses.size() >= 6) {
						viewHolder.mTv1.setText(customerses.get(0).getName());
						viewHolder.mTv2.setText(customerses.get(1).getName());
						viewHolder.mTv3.setText(customerses.get(2).getName());
						viewHolder.mTv4.setText(customerses.get(3).getName());
						viewHolder.mTv5.setText(customerses.get(4).getName());
						viewHolder.mTv6.setText(customerses.get(5).getName());
						viewHolder.mTv1.getBackground().setAlpha(255);
						viewHolder.mTv2.getBackground().setAlpha(255);
						viewHolder.mTv3.getBackground().setAlpha(255);
						viewHolder.mTv4.getBackground().setAlpha(255);
						viewHolder.mTv5.getBackground().setAlpha(255);
						viewHolder.mTv6.getBackground().setAlpha(255);

					}
				}
				viewHolder.mNameTv.setText(list.get(position).getName());
			}


			if (list.get(position) != null && list.get(position).getUrl() != null) {
				BaseApplication.getApplicationInstance().displayImg(list.get(position).getUrl() , viewHolder.mIv);
			}

			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, (int) (0.2 * height));
			convertView.setLayoutParams(lp);
		}
		return convertView;
	}

	class ViewHolder {
		RelativeLayout mLayout;
		TextView mNameTv , mTv1 , mTv2 , mTv3 , mTv4 , mTv5 , mTv6;
		ImageView mIv;
		LinearLayout mTvLayout;

	}
}
